package com.dmcb.trade.business.crawlers.article;

import com.dmcb.common.business.utils.DateUtil;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import com.dmcb.trade.business.entities.ArticleUrl;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 百度百家文章解析
 * Created by Administrator on 2016/5/4.
 */
@Service
public class BaiDuBaiJiaParser extends BaseParser {

    /**
     * 网站URL网址
     *
     * @return 链接
     */
    @Override
    protected String url() {
        return CrawlConstant.BAIDUBAIJIA_URL;
    }

    /**
     * 获取标题选择器
     *
     * @return 标题选择器
     */
    @Override
    protected String titleSelect() {
        return "h1.titleSize,h1.reader_ab_test span,div.header>h2,h2.text-title,h3.post-title,div.header>h2,div#page>h1,h1.title,div.article-title>h2,h1.mth-header,div.info>div.title,h1.page-title>div.element,span.titleTxt";
    }

    /**
     * 获取日期选择器
     *
     * @return 发布日期选择器
     */
    @Override
    protected String dateSelect() {
        return "div.article-info>span.time,iv.article-source,span.read,div.info,div.article-source";
    }

    /**
     * 获取正文内容选择器
     *
     * @return 正文内容选择器
     */
    @Override
    protected String bodySelect() {
        return "div.l-main-col,div#content,div.article-content,div.mth-editor-content";
    }

    /**
     * 获取封面选择器
     *
     * @return 封面选择器
     */
    @Override
    protected String coverSelect() {
        return "div.article-detail>p.image img[src],div.mth-editor-content img[src],div#content>div>div>div img[src],div.article-content>div img[src]";
    }

    /**
     * 获取作者选择器
     *
     * @return 作者选择器
     */
    @Override
    protected String authorNameSelect() {
        return "div.article-info> a.name,div.name,p.author-name,div.name";
    }

    /**
     * 设置发布时间
     *
     * @param doc     网页
     * @param article 文章
     */
    @Override
    protected void setPostDate(Document doc, Article article) {
        Element element = doc.select(dateSelect()).first();
        if (element != null) {
            element.select("span.source").remove();
            String time = text(element);
            Date date = DateUtil.parseDate(time);
            if (date != null) {
                article.setPostTime(date);
            }
        }
    }

    @Override
    protected boolean isJsEnabled() {
           return false;
    }

    @Override
    protected String platform() {
        return "百度百家";
    }
}
